import React, { useEffect,useState } from 'react';
import { Form, Input, Select, Button, notification } from 'antd';
import { DatePicker, Space } from 'antd';
import '../../../scss/lhq/supplieredit.scss'
import { useNavigate, useLocation } from 'react-router-dom'
import { shopping_edit} from '../../../tools/lhq/api'

function Supplieredit(props) {
    const [date, setDate] = useState()
    const [from] = Form.useForm();
    // 接收state参数
    var location = useLocation()
    useEffect(() => {
        from.setFieldsValue({
            id: location.state.id,
            date: location.state.date,
            cnumber: location.state.cnumber,
            status: location.state.status,
            gnumber: location.state.gnumber,
            name: location.state.name,
            response: location.state.response,
            deal: location.state.deal,
            sprocess: location.state.sprocess,
            mprocess: location.state.mprocess,
        })
    }, [])
    // 日期
    const onChange = (date, dateString) => {
        setDate(dateString);
    };

    var navigate = useNavigate()
    const onFinish = (values) => {
        setDate(date);
        shopping_edit({ id: location.state.id, date: date, cnumber: values.cnumber, 
            status: values.status, gnumber: values.gnumber, name: values.name, response:values.response,
            deal: values.deal, sprocess: values.sprocess, mprocess: values.mprocess}).then((res) => {
            notification.success({ message: '编辑成功!' });
            navigate('/index/shopping')
        })
    };

    // 保存编辑
    return (
        <div className='supplieredit'>
            <Form form={from} className='form' initialValues={{ remember: true }} onFinish={onFinish} labelCol={{ span: 6 }}
                wrapperCol={{ span: 18 }}>
                <Form.Item className='item' label="日期" name="date">
                    <Space direction="vertical">
                        <DatePicker onChange={onChange} />
                    </Space>
                </Form.Item>
                <Form.Item className='item' label="采购单编号" name="cnumber">
                    <Input placeholder='请输入采购单编号' />
                </Form.Item>
                <Form.Item className='item' label="采购单状态" name="status">
                    <Select
                        placeholder="所有"
                        style={{ width: 120 }}
                        options={[
                            { value: '待分配' },
                            { value: '已分配' },
                            { value: '已完成' }
                        ]}
                    />
                </Form.Item>
                <Form.Item className='item' label="供应商编号" name="gnumber">
                    <Input placeholder='请输入供应商编号' />
                </Form.Item>
                <Form.Item className='item' label="供应商名称" name="name">
                    <Input placeholder='请输入单位名称' />
                </Form.Item>
                <Form.Item className='item' label="请购负责人" name="response">
                    <Input placeholder='请输入请购负责人' />
                </Form.Item>
                <Form.Item className='item' label="采购处理人" name="deal">
                    <Input placeholder='请输入采购处理人' />
                </Form.Item>
                <Form.Item className='item' label="采购审核人" name="sprocess">
                    <Input placeholder='请输入采购审核人' />
                </Form.Item>
                <Form.Item className='item' label="财务审核人" name="mprocess">
                    <Input placeholder='请输入财务审核人' />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 6, span: 18 }}>
                    <Button type="primary" htmlType="submit">保存</Button>
                </Form.Item>
            </Form>
        </div>
    );
}

export default Supplieredit;